<template>
  <el-container v-loading="loading">
    <el-table
      :data="tableData"
      tooltip-effect="dark"
      border
      :default-sort="{prop: 'date', order: 'descending'}"
      size="small">
      <el-table-column
        prop="truename"
        width="120"
        label="姓名">
      </el-table-column>
      <el-table-column
        prop="phone"
        label="电话">
      </el-table-column>
      <el-table-column
        prop="create_time"
        label="申请时间	">
      </el-table-column>
      <el-table-column
        label="操作"
        fixed="right"
        width="300">
        <template slot-scope="scope">
          <el-button
            icon="fa fa-fw fa-check"
            size="mini"
            type="success"
            title="同意"
            @click="handleApplyAcceptItem(scope.$index, scope.row)">
            同意
          </el-button>

          <el-button
            icon="fa fa-fw fa-remove"
            size="mini"
            type="danger"
            title="拒绝"
            @click="handleApplyReject(scope.$index, scope.row)">
            拒绝
          </el-button>

        </template>
      </el-table-column>
    </el-table>

    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="pagination.currentPage"
      :page-sizes="pagination.per_page"
      :page-size="pagination.per_page"
      :total="pagination.total"
      layout="total, sizes, prev, pager, next, jumper">
    </el-pagination>


  </el-container>
</template>

<script>
  export default {
    mounted: function () {
      this.getData();
    },
    methods: {
      /**
       * 数据请求
       * */
      getData: function () {
        let _this = this;
        this.api.getDistributionApply({
          page: this.pagination.currentPage,
          p: this.pagination.pageSize
        }).then(function (res) {
          if (res.code == 1) {
            _this.tableData = res.data.data;
						_this.pagination.total = res.data.total;
          }
          _this.loading = false;
        }).catch(function (res) {
          throw new Error(res);
        });
      },
      /**
       * 分页数改变
       */
      handleSizeChange: function (pageSize) {
        this.pagination.pageSize = pageSize;
        this.getData();
      },
      /**
       * 当前页面改变
       * @param currentPage
       */
      handleCurrentChange: function (currentPage) {
        this.pagination.currentPage = currentPage;
        this.getData();
      },

      /**
       * 同意申请
       */
      handleApplyAcceptItem(index, row) {
        let _this = this;
        let _id = row.id;
        this.$confirm('此操作将同意该申请, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          _this.api.getDistributionDual({id: _id, status: 1}).then(function (res) {
            if (res.code == 1) {
              _this.getData();
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'success'
              });
            } else {
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'warning'
              });
            }
          }).catch(function (error) {
            throw new Error(error);
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消该操作'
          })
        })
      },
      /**
       * 拒绝申请
       * @param index
       * @param row
       */
      handleApplyReject(index, row) {
        let _this = this;
        let _id = row.id;
        this.$prompt('请输入拒绝原因', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
        }).then(({value}) => {
          _this.api.getDistributionDual({id: _id, status: 2, reason: value}).then(function (res) {
            if (res.code == 1) {
              _this.getData();
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'success'
              });
            } else {
              _this.$message({
                showClose: true,
                message: res.msg,
                type: 'warning'
              });
            }
          }).catch(function (error) {
            throw new Error(error);
          });
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消操作'
          });
        });
      },


    },
    data() {
      return {
        loading: true,  // 页面加载
        tableData: [],
        multipleSelection: [],
        pagination: {
          total: 5,
          currentPage: 1,
          pageSize: 10,
          pageSizes: [5, 10, 20, 25, 30, 50, 100],
        },
        dialogVisible: false,   // 弹框
        currentDataIndex: null,
      }
    },
  }
</script>

<style scoped>
  .el-container {
    display: block;
  }

  .el-table, .el-row {
    width: 98% !important;
    margin: 15px auto;
  }

  .el-pagination {
    text-align: center;
    margin: 15px 0;
  }

  .el-table .success-row {
    background: #f0f9eb;
  }

  .logo-img {
    width: 40px;
    height: 40px;
  }

  .label {
    width: 100px;
    display: inline-block;
    text-align: right;
    margin-right: 15px;
  }

  .big-img {
    width: 400px;
    height: 200px;
  }
</style>
